package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.AliyunVodApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class AliyunCodController implements AliyunVodApi {

    @Value("${aliyun.region}")
    private String region;

    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    /**
     * 获取音视频上传凭证等信息
     *
     * @param request
     * @return
     */
    @PostMapping("media/vod-token")
    public VodUploadToken generateUploadToken(@RequestBody VodUploadRequest request) {
        try {
            // 初始化客户端
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            // 客户端发送上传请求并获取响应
            CreateUploadVideoResponse response = AliyunVODUtil.createUploadVideo(client, request.getTitle(), request.getFileName());
            // 封装响应数据
            VodUploadToken token = new VodUploadToken();
            token.setVideoId(response.getVideoId());
            token.setUploadAddress(response.getUploadAddress());
            token.setUploadAuth(response.getUploadAuth());
            token.setRequestId(response.getRequestId());
            return token;
        } catch (Exception e) {
            log.info("获取上传凭证失败 => {}", e.getMessage());
            ExceptionCast.cast(CommonErrorCode.UNKOWN);
            return null;
        }
    }

    /**
     * 刷新音视频上传凭证
     *
     * @param videoId
     * @return
     */
    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshUploadToken(@PathVariable("videoId") String videoId) {
        try {
            // 初始化客户端
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            // 客户端发送刷新凭证请求并获取响应
            RefreshUploadVideoResponse response = AliyunVODUtil.refreshUploadVideo(client, videoId);
            // 封装响应数据
            VodUploadToken token = new VodUploadToken();
            token.setVideoId(response.getVideoId());
            token.setUploadAddress(response.getUploadAddress());
            token.setUploadAuth(response.getUploadAuth());
            token.setRequestId(response.getRequestId());
            return token;
        } catch (Exception e) {
            log.info("刷新凭证失败 => {}", e.getMessage());
            ExceptionCast.cast(CommonErrorCode.UNKOWN);
            return null;
        }
    }
}
